package com.fenrirtec.aepp.common.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fenrirtec.aepp.common.condition.TenderCondition;
import com.fenrirtec.aepp.common.dao.TenderDao;
import com.fenrirtec.aepp.common.dao.impl.TenderDao_JDBCImpl;
import com.fenrirtec.aepp.common.dto.MemberDto;
import com.fenrirtec.aepp.common.dto.TenderDetailDto;
import com.fenrirtec.aepp.common.dto.TenderInviteDto;
import com.fenrirtec.aepp.common.dto.TenderMemberStatusDto;
import com.fenrirtec.aepp.common.dto.TenderProductDto;
import com.fenrirtec.aepp.common.dto.TenderQuoteDetailDto;
import com.fenrirtec.aepp.common.dto.TenderQuoteDto;
import com.fenrirtec.aepp.common.dto.TenderVenderDto;
import com.fenrirtec.aepp.common.model.Member;
import com.fenrirtec.aepp.common.model.TenderBid;
import com.fenrirtec.aepp.common.model.TenderDetail;
import com.fenrirtec.aepp.common.model.TenderEnroll;
import com.fenrirtec.aepp.common.model.TenderFinalist;
import com.fenrirtec.aepp.common.model.TenderInvite;
import com.fenrirtec.aepp.common.model.TenderMemberStatus;
import com.fenrirtec.aepp.common.model.TenderProduct;
import com.fenrirtec.aepp.common.model.TenderQuote;
import com.fenrirtec.aepp.common.model.TenderQuoteDetail;
import com.fenrirtec.aepp.common.service.TenderService;
import com.fenrirtec.core.database.DatabaseSessionManager;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.BeanCopierUtils;
import com.fenrirtec.core.utils.DatabaseUtils;

public class TenderServiceImpl implements TenderService {

	private static final Logger logger = LoggerFactory.getLogger(TenderServiceImpl.class);

	@Override
	public Integer countByCnd(TenderCondition condition) {
		if (logger.isInfoEnabled()) {
			logger.info("[TenderServiceImpl#countByCnd] start.");
		}

		DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
		sessionManager.open();
		Connection connection = sessionManager.getSession();

		try {
			connection.setAutoCommit(false);
			TenderDao tenderDao = new TenderDao_JDBCImpl();
			Integer count = tenderDao.countByCnd(condition);
			connection.commit();
			return count;
		} catch (DatabaseException e) {
			// expected
			if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#countByCnd] database error occurred.", e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw e;
		} catch (Exception e) {
			// unexpected
			if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#countByCnd] unexpected error occurred.", e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw new UnexpectedException("unexpected error.", e);
		} finally {
			if (logger.isInfoEnabled()) {
				logger.info("[TenderServiceImpl#countByCnd] finish.");
			}
			sessionManager.close();
		}
	}

    @Override
    public List<TenderDetailDto> searchByCnd(TenderCondition condition) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#searchByCnd] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderDetail> tenderDetailList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderDetailList = tenderDao.searchByCnd(condition);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#searchByCnd] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#searchByCnd] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#searchByCnd] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderDetailDto> tenderDetailDtoList = null;
        
        if (tenderDetailList != null && tenderDetailList.size() > 0) {
            tenderDetailDtoList = new ArrayList<TenderDetailDto>();
            for (TenderDetail tenderDetail : tenderDetailList) {
                TenderDetailDto tenderDetailDto = new TenderDetailDto();
                BeanCopierUtils.copyProperties(tenderDetail, tenderDetailDto);
                tenderDetailDtoList.add(tenderDetailDto);
            }
        }
        
        return tenderDetailDtoList;
    }

    @Override
    public Integer publish(TenderDetailDto tenderDetailDto, List<TenderProductDto> tenderProductDtoList, List<TenderVenderDto> tenderVenderDtoList, String createUser) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#publish] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;
        Integer tenderId = null;
        
        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            TenderDetail tenderDetail = new TenderDetail();
            BeanCopierUtils.copyProperties(tenderDetailDto, tenderDetail);
            result = tenderDao.insertTenderInformation(tenderDetail, createUser);
            if (result) {
                tenderId = DatabaseUtils.getLastInsertId(connection);
                if (tenderId > 0) {
                    for (TenderProductDto tenderProductDto : tenderProductDtoList) {
                        TenderProduct tenderProduct = new TenderProduct();
                        BeanCopierUtils.copyProperties(tenderProductDto, tenderProduct);
                        tenderProduct.setTenderId(tenderId);
                        result = tenderDao.insertTenderProduct(tenderProduct, createUser);
                        if (!result) {
                            break;
                        }
                    }
                    
                    if (result) {
                        if (tenderDetailDto.getTenderPattern() == 2 && tenderVenderDtoList != null && !tenderVenderDtoList.isEmpty()) {
                            for (TenderVenderDto tenderVenderDto : tenderVenderDtoList) {
                                TenderEnroll tenderEnroll = new TenderEnroll();
                                tenderEnroll.setTenderId(tenderId);
                                tenderEnroll.setMemberLoginName(tenderVenderDto.getMemberLoginName());
                                tenderEnroll.setFinalistFlag(true);
                                tenderEnroll.setInviteFlag(true);
                                result = tenderDao.insertTenderEnroll(tenderEnroll, createUser);
                                if (!result) {
                                    break;
                                }
                                TenderFinalist tenderFinalist = new TenderFinalist();
                                tenderFinalist.setTenderId(tenderId);
                                tenderFinalist.setMemberLoginName(tenderVenderDto.getMemberLoginName());
                                tenderFinalist.setQuoteFlag(false);
                                tenderFinalist.setInviteFlag(true);
                                result = tenderDao.insertTenderFinalist(tenderFinalist, createUser);
                                if (!result) {
                                    break;
                                }
                            }
                            if (!result) {
                                connection.rollback();
                            }
                        }
                    } else {
                        connection.rollback();
                    }
                } else {
                    result = false;
                    connection.rollback();
                }
            } else {
                connection.rollback();
            }
            
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#publish] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#publish] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#publish] finish.");
            }
            sessionManager.close();
        }
        return tenderId;
    }

    @Override
    public List<TenderProductDto> findProduct(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findProduct] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderProduct> tenderProductList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderProductList = tenderDao.findProduct(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findProduct] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findProduct] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findProduct] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderProductDto> tenderProductDtoList = null;
        
        if (tenderProductList != null && tenderProductList.size() > 0) {
            tenderProductDtoList = new ArrayList<TenderProductDto>();
            for (TenderProduct tenderProduct : tenderProductList) {
                TenderProductDto tenderProductDto = new TenderProductDto();
                BeanCopierUtils.copyProperties(tenderProduct, tenderProductDto);
                tenderProductDtoList.add(tenderProductDto);
            }
        }
        
        return tenderProductDtoList;
    }

    @Override
    public TenderDetailDto findDetail(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findDetail] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        TenderDetail tenderDetail = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderDetail = tenderDao.findDetail(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findDetail] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findDetail] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findDetail] finish.");
            }
            sessionManager.close();
        }
        
        TenderDetailDto tenderDetailDto = null;
        
        if (tenderDetail != null) {
            tenderDetailDto = new TenderDetailDto();
            BeanCopierUtils.copyProperties(tenderDetail, tenderDetailDto);
        }
        
        return tenderDetailDto;
    }

    @Override
    public Boolean enroll(Integer tenderId, String memberLoginName) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#enroll] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            TenderEnroll tenderEnroll = new TenderEnroll();
            tenderEnroll.setTenderId(tenderId);
            tenderEnroll.setMemberLoginName(memberLoginName);
            tenderEnroll.setFinalistFlag(false);
            tenderEnroll.setInviteFlag(false);
            result = tenderDao.insertTenderEnroll(tenderEnroll, memberLoginName);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#enroll] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#enroll] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#enroll] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public TenderMemberStatusDto findTenderMemberStatus(Integer tenderId, String memberLoginName) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findTenderMemberStatus] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        TenderMemberStatus tenderMemberStatus = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderMemberStatus = tenderDao.findTenderMemberStatus(tenderId, memberLoginName);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findTenderMemberStatus] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findTenderMemberStatus] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findTenderMemberStatus] finish.");
            }
            sessionManager.close();
        }
        
        TenderMemberStatusDto tenderMemeberStatusDto = null;
        
        if (tenderMemberStatus != null) {
            tenderMemeberStatusDto = new TenderMemberStatusDto();
            BeanCopierUtils.copyProperties(tenderMemberStatus, tenderMemeberStatusDto);
        }
        
        return tenderMemeberStatusDto;
    }

    @Override
    public Boolean quote(TenderQuoteDto tenderQuoteDto, List<TenderQuoteDetailDto> tenderQuoteDetailDtoList) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#enroll] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            TenderQuote tenderQuote = new TenderQuote();
            BeanCopierUtils.copyProperties(tenderQuoteDto, tenderQuote);
            result = tenderDao.insertTenderQuote(tenderQuote);
            
            if (!result) {
                connection.rollback();
                return false;
            }
            
            for (TenderQuoteDetailDto tenderQuoteDetailDto : tenderQuoteDetailDtoList) {
                TenderQuoteDetail tenderQuoteDetail = new TenderQuoteDetail();
                BeanCopierUtils.copyProperties(tenderQuoteDetailDto, tenderQuoteDetail);
                result = tenderDao.insertTenderQuoteDetail(tenderQuoteDetail);
                if (!result) {
                    connection.rollback();
                    return false;
                }
            }
            
            result = tenderDao.updateQuoteFlag(tenderQuote.getTenderId(), tenderQuote.getMemberLoginName(), true, tenderQuote.getUpdateUser());
            
            if (!result) {
                connection.rollback();
                return false;
            }
            
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#enroll] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#enroll] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#enroll] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public List<TenderInviteDto> findInvite(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findInvite] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderInvite> tenderInviteList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderInviteList = tenderDao.findInvite(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findInvite] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findInvite] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findInvite] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderInviteDto> tenderInviteDtoList = null;
        if (tenderInviteList != null && tenderInviteList.size() > 0) {
            tenderInviteDtoList = new ArrayList<TenderInviteDto>();
            for (TenderInvite tenderInvite : tenderInviteList) {
                TenderInviteDto tenderInviteDto = new TenderInviteDto();
                BeanCopierUtils.copyProperties(tenderInvite, tenderInviteDto);
                tenderInviteDtoList.add(tenderInviteDto);
            }
        }
        
        return tenderInviteDtoList;
    }

    @Override
    public Boolean update(TenderDetailDto tenderDetailDto, List<TenderProductDto> tenderProductDtoList, List<TenderVenderDto> tenderVenderDtoList, String updateUser) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#update] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;
        
        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            TenderDetail tenderDetail = new TenderDetail();
            Integer tenderId = tenderDetailDto.getTenderId();
            BeanCopierUtils.copyProperties(tenderDetailDto, tenderDetail);
            result = tenderDao.updateTenderInformation(tenderDetail, updateUser);
            if (result) {
                result = tenderDao.deleteTenderProduct(tenderId);
                if (result) {
                    for (TenderProductDto tenderProductDto : tenderProductDtoList) {
                        TenderProduct tenderProduct = new TenderProduct();
                        BeanCopierUtils.copyProperties(tenderProductDto, tenderProduct);
                        tenderProduct.setTenderId(tenderId);
                        result = tenderDao.insertTenderProduct(tenderProduct, updateUser);
                        if (!result) {
                            break;
                        }
                    }
                    
                    if (result) {
                        List<TenderInvite> tenderInviteList = tenderDao.findInvite(tenderId);
                        if (tenderInviteList != null && tenderInviteList.size() > 0) {
                            result = tenderDao.deleteTenderInvite(tenderId);
                            if (result) {
                                if (tenderDetailDto.getTenderPattern() == 2 && tenderVenderDtoList != null && !tenderVenderDtoList.isEmpty()) {
                                    for (TenderVenderDto tenderVenderDto : tenderVenderDtoList) {
                                        TenderEnroll tenderEnroll = new TenderEnroll();
                                        tenderEnroll.setTenderId(tenderId);
                                        tenderEnroll.setMemberLoginName(tenderVenderDto.getMemberLoginName());
                                        tenderEnroll.setFinalistFlag(true);
                                        tenderEnroll.setInviteFlag(true);
                                        result = tenderDao.insertTenderEnroll(tenderEnroll, updateUser);
                                        if (!result) {
                                            break;
                                        }
                                        TenderFinalist tenderFinalist = new TenderFinalist();
                                        tenderFinalist.setTenderId(tenderId);
                                        tenderFinalist.setMemberLoginName(tenderVenderDto.getMemberLoginName());
                                        tenderFinalist.setQuoteFlag(false);
                                        tenderFinalist.setInviteFlag(true);
                                        result = tenderDao.insertTenderFinalist(tenderFinalist, updateUser);
                                        if (!result) {
                                            break;
                                        }
                                    }
                                    if (!result) {
                                        connection.rollback();
                                    }
                                }
                            } else {
                                connection.rollback();
                            }
                        }
                    } else {
                        connection.rollback();
                    }
                } else {
                    connection.rollback();
                }
            } else {
                connection.rollback();
            }
            
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#update] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#update] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#update] finish.");
            }
            sessionManager.close();
        }
        return result;
    }

    @Override
    public Boolean delete(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#delete] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;
        
        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            
            result = tenderDao.deleteTenderInvite(tenderId);
            if (!result) {
                connection.rollback();
                return result;
            }
            
            result = tenderDao.deleteTenderProduct(tenderId);
            if (!result) {
                connection.rollback();
                return result;
            }
            
            result = tenderDao.deleteTenderInformation(tenderId);
            if (!result) {
                connection.rollback();
                return result;
            }
            
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#delete] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#delete] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#delete] finish.");
            }
            sessionManager.close();
        }
        return result;
    }

    @Override
    public Boolean changeStatus(Integer tenderId, Integer status, String updateUser) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#changeStatus] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;
        
        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            
            result = tenderDao.changeStatus(tenderId, status, updateUser);
            if (!result) {
                connection.rollback();
                return result;
            }
            
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#changeStatus] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#changeStatus] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#changeStatus] finish.");
            }
            sessionManager.close();
        }
        return result;
    }

    @Override
    public List<MemberDto> findEnrollMember(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findEnrollMember] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<Member> memberList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            memberList = tenderDao.findEnrollMember(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findEnrollMember] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findEnrollMember] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findEnrollMember] finish.");
            }
            sessionManager.close();
        }
        
        List<MemberDto> memberDtoList = null;
        
        if (memberList != null && memberList.size() > 0) {
            memberDtoList = new ArrayList<MemberDto>();
            for (Member member : memberList) {
                MemberDto memberDto = new MemberDto();
                BeanCopierUtils.copyProperties(member, memberDto);
                memberDtoList.add(memberDto);
            }
        }
        
        return memberDtoList;
    }

    @Override
    public List<MemberDto> findFinalistMember(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findFinalistMember] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<Member> memberList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            memberList = tenderDao.findFinalistMember(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findFinalistMember] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findFinalistMember] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findFinalistMember] finish.");
            }
            sessionManager.close();
        }
        
        List<MemberDto> memberDtoList = null;
        
        if (memberList != null && memberList.size() > 0) {
            memberDtoList = new ArrayList<MemberDto>();
            for (Member member : memberList) {
                MemberDto memberDto = new MemberDto();
                BeanCopierUtils.copyProperties(member, memberDto);
                memberDtoList.add(memberDto);
            }
        }
        
        return memberDtoList;
    }

    @Override
    public List<MemberDto> findQuoteMember(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findQuoteMember] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<Member> memberList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            memberList = tenderDao.findQuoteMember(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findQuoteMember] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findQuoteMember] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findQuoteMember] finish.");
            }
            sessionManager.close();
        }
        
        List<MemberDto> memberDtoList = null;
        
        if (memberList != null && memberList.size() > 0) {
            memberDtoList = new ArrayList<MemberDto>();
            for (Member member : memberList) {
                MemberDto memberDto = new MemberDto();
                BeanCopierUtils.copyProperties(member, memberDto);
                memberDtoList.add(memberDto);
            }
        }
        
        return memberDtoList;
    }

    @Override
    public MemberDto findBidMember(Integer tenderId) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findBidMember] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Member member = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            member = tenderDao.findBidMember(tenderId);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findBidMember] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findBidMember] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findBidMember] finish.");
            }
            sessionManager.close();
        }
        
        MemberDto memberDto = null;
        if (member != null) {
            memberDto = new MemberDto();
            BeanCopierUtils.copyProperties(member, memberDto);
        }
        
        return memberDto;
    }

    @Override
    public Boolean finalist(Integer tenderId, String[] members, String createUser) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#finalist] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            for (String memberLoginName : members) {
                TenderFinalist tenderFinalist = new TenderFinalist();
                tenderFinalist.setTenderId(tenderId);
                tenderFinalist.setMemberLoginName(memberLoginName);
                tenderFinalist.setQuoteFlag(false);
                tenderFinalist.setInviteFlag(false);
                tenderFinalist.setCreateUser(createUser);
                result = tenderDao.insertTenderFinalist(tenderFinalist, createUser);
                if (!result) {
                    connection.rollback();
                    break;
                }
                result = tenderDao.updateFinalistFlag(tenderId, memberLoginName, true, createUser);
                if (!result) {
                    connection.rollback();
                    break;
                }
            }
            if (result) {
                connection.commit();
            }
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#finalist] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#finalist] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#finalist] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public Boolean awardBid(Integer tenderId, String memberLoginName, String createUser) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#awardBid] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            TenderBid tenderBid = new TenderBid();
            tenderBid.setTenderId(tenderId);
            tenderBid.setMemberLoginName(memberLoginName);
            tenderBid.setCreateUser(createUser);
            result = tenderDao.insertTenderBid(tenderBid, createUser);
            if (!result) {
                connection.rollback();
                return result;
            }
            result = tenderDao.updateBidFlag(tenderId, memberLoginName, true, createUser);
            if (!result) {
                connection.rollback();
                return result;
            }
            if (result) {
                connection.commit();
            }
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#awardBid] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#awardBid] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#awardBid] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public Boolean withdrawal(Integer tenderId, String memberLoginName, String updateUser) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#withdrawal] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        Boolean result = false;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            result = tenderDao.deleteTenderBid(tenderId, memberLoginName);
            if (!result) {
                connection.rollback();
                return result;
            }
            result = tenderDao.updateBidFlag(tenderId, memberLoginName, false, updateUser);
            if (!result) {
                connection.rollback();
                return result;
            }
            if (result) {
                connection.commit();
            }
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#withdrawal] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#withdrawal] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#withdrawal] finish.");
            }
            sessionManager.close();
        }
        
        return result;
    }

    @Override
    public List<TenderQuoteDetailDto> findQuoteDetail(Integer tenderId, String memberLoginName) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findQuoteDetail] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderQuoteDetail> tenderQuoteDetailList = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderQuoteDetailList = tenderDao.findQuoteDetail(tenderId, memberLoginName);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findQuoteDetail] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findQuoteDetail] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findQuoteDetail] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderQuoteDetailDto> tenderDetailDtoList = null;
        
        if (tenderQuoteDetailList != null && tenderQuoteDetailList.size() > 0) {
            tenderDetailDtoList = new ArrayList<TenderQuoteDetailDto>();
            for (TenderQuoteDetail tenderQuoteDetail : tenderQuoteDetailList) {
                TenderQuoteDetailDto tenderQuoteDetailDto = new TenderQuoteDetailDto();
                BeanCopierUtils.copyProperties(tenderQuoteDetail, tenderQuoteDetailDto);
                tenderDetailDtoList.add(tenderQuoteDetailDto);
            }
        }
        
        return tenderDetailDtoList;
    }

    @Override
    public TenderQuoteDto findQuote(Integer tenderId, String memberLoginName) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderServiceImpl#findQuote] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        TenderQuote tenderQuote = null;

        try {
            connection.setAutoCommit(false);
            TenderDao tenderDao = new TenderDao_JDBCImpl();
            tenderQuote = tenderDao.findQuote(tenderId, memberLoginName);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findQuote] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderServiceImpl#findQuote] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderServiceImpl#findQuote] finish.");
            }
            sessionManager.close();
        }
        
        TenderQuoteDto tenderQuoteDto = null;
        if (tenderQuote != null) {
            tenderQuoteDto = new TenderQuoteDto();
            BeanCopierUtils.copyProperties(tenderQuote, tenderQuoteDto);
        }
        
        return tenderQuoteDto;
    }
}
